from typing import Optional, List, Union
from fastapi import APIRouter, status, Form, File,UploadFile
from pydantic import BaseModel, EmailStr

app04 = APIRouter()


# 响应模型类
class UserIn(BaseModel):
    username: str
    password: str
    email: EmailStr
    mobile: str = "10086"
    address: str = None
    full_name: Optional[str] = None


class UserOut(BaseModel):
    username: str
    email: EmailStr
    mobile: str = "10086"
    address: str = None
    full_name: Optional[str] = None


users = {
    "user01": {"username": "球球", "password": 123, "email": "421967575@qq.com", "address": '河北'},
    "user00": {"username": "张张", "password": 456, "email": "152635988@qq.com", "address": '秦皇岛',
               'mobile': 110}
}


@app04.post('/response_model', response_model=UserOut, response_model_exclude_unset=True)
async def response_model(user: UserIn):
    print(user.password)
    return users['user00']


@app04.post(
    '/response_model/attributes',
    response_model=Union[UserIn, UserOut]
)
async def response_model_attributes(user: UserIn):
    return users['user00']


# 响应状态码
@app04.post('/status_code', status_code=200)
async def status_code():
    return {'status_code': 200}


@app04.post('/status_attribute', status_code=status.HTTP_200_OK)
async def status_attribute():
    print(type(status.HTTP_200_OK))
    return {'status_code': status.HTTP_200_OK}


# 表单数据处理
@app04.post('/login')
async def login(username: str = Form(...), password: str = Form(...)):
    return {'username': username, 'password': password}


# 单文件、多文件上传及参数详解
@app04.post('/file')
async def file_1(file: bytes = File(...)):
    return {'file_size': len(file)}